package com.alibaba.cloud.boot.stock.service.impl;

import com.alibaba.cloud.boot.stock.entity.TStock;
import com.alibaba.cloud.boot.stock.mapper.TStockMapper;
import com.alibaba.cloud.boot.stock.service.ITStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xrr
 * @since 2024-02-13
 */
@Service
public class TStockServiceImpl extends ServiceImpl<TStockMapper, TStock> implements ITStockService {

    @Autowired
    private TStockMapper stockMapper;

    @Override
    public String reduceStockCount(int stockId, int stockCount) {
        TStock stock = stockMapper.selectById(stockId);
        if (stock == null){
            return "库存不存在";
        }

        TStock updatingStock =new TStock();
        updatingStock.setStockId(stockId);
        updatingStock.setStockCount(stock.getStockCount()-stockCount);
        int affectCount =  stockMapper.updateById(updatingStock);
        return "更新成功，更新影响行数为:"+affectCount;
    }

    @Override
    public String setStockCount(int stockId, int stockCount) {
        TStock stock = stockMapper.selectById(stockId);
        if (stock == null){
            return "库存不存在";
        }

        TStock updatingStock =new TStock();
        updatingStock.setStockId(stockId);
        updatingStock.setStockCount(stockCount);
        int affectCount =  stockMapper.updateById(updatingStock);
        return "更新成功，更新影响行数为:"+affectCount;
    }
}
